package com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveUser;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.repository.EarEveUserRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @Author:
 * @CreateTime: 2023/7/28 16:46
 * @Description:
 */
@Repository
public class EarEveUserRepositoryImpl implements EarEveUserRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public EarEveUser selWheHavCurEvent(String userId) {
        String sql="select * from xczh_seismic_event_user where  user_id= ?";
        EarEveUser earEveUser=new EarEveUser();
        try {
            earEveUser = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(EarEveUser.class),userId);
            return earEveUser;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public void save(EarEveUser earEveUser) {
        String sql="insert into xczh_seismic_event_user(id,user_id,xsem_id,is_current_event) " +
                "values(:id, :userId, :xsemId, :isCurrentEvent)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(earEveUser);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void updateCurEvent(String xsemId,String userId) {
        String sql="update xczh_seismic_event_user set xsem_id = ? where user_id= ?";
        jdbcTemplate.update(sql,xsemId,userId);
    }
}
